/**
 * <p> title �� StatisticsUtil.java</p>
 * <p> package �� cn.edu.hit.education.utils</p>
 * <p> description ��TODO</p>
 * <p> copyright �� ��������ҵ��ѧ(C) 2019</p>
 * <p> company �� ��������ҵ��ѧAS&MT</p>
 * <p> author �� ��ѩ�� jinxuesong@163.com</p>
 * <p> date �� 2021��4��22�� ����11:28:52</p>
 * <p> version �� v1.0 </p>
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2021��4��22��       ��ѩ��          1.0             1.0
 * Why & What is modified: <�޸�ԭ������>
 */
package cn.edu.hit.education.utils;

import java.text.DecimalFormat;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import cn.edu.hit.education.pojo.Course;
import cn.edu.hit.education.pojo.Property;
import cn.edu.hit.education.service.ICourseService;
import cn.edu.hit.education.service.IPropertyService;
import educationschedule.Application;

public class CourseUtil {
	public static void codeCourse() {
		DecimalFormat df = new DecimalFormat("00");
		ICourseService courseService = (ICourseService) Application.applicationContext
				.getBean("courseServiceImpl");
		IPropertyService propertyService = (IPropertyService) Application.applicationContext
				.getBean("propertyServiceImpl");

		propertyService.clearPropertyNum();

		List<Course> courses = courseService.queryAllCourse();
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

		try {

			IRunnableWithProgress runnable = new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) {
					//List<Course> courses = courseService.queryAllCourse();
					monitor.beginTask("开始编码课程......", courses.size());
					String strCourseCode = "";
					if (courses != null && courses.size() > 0) {
						for (Course course : courses) {
							strCourseCode = "";
							if (monitor.isCanceled()) 
								return;
							
							Property property = propertyService.queryPropertyById(course.getPropertyId());
							strCourseCode += property.getCode();
							strCourseCode += df.format(9);
							strCourseCode += String.valueOf(1);
							strCourseCode += df.format(property.getNum() + 1);
							course.setNumber(strCourseCode);

							property.setNum(property.getNum() + 1);
							propertyService.update(property);

							courseService.update(course);
							monitor.worked(1);
						}
					}
					monitor.done();
				}
			};
			try {
				new ProgressMonitorDialog(parentShell)
						.run(true, true, runnable);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		} catch (Exception e) {

		}

	}
}
